package com.daoeping.dao;

import java.util.List;

import org.json.JSONArray;

import com.daoeping.domain.Bzn_commentdraft;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;

public class CommentDraftDao extends AllDaoSupport {

	@SuppressWarnings("unchecked")
	public JsonData pageBzn_commentdrafts(Page page, TableData tableData, Bzn_commentdraft bzn_commentdraft){
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			sb1.append("select  bzn.title , bzn.content , user.username,  bzn.id ,user.id  from Bzn_commentdraft bzn , Usr_user  user where user.id = bzn.usr_user.id ");
			sb2.append("select count(bzn) from Bzn_commentdraft bzn ");
			if(bzn_commentdraft!=null){
				if (bzn_commentdraft.getUsr_user().getId()!=null&&!"".equals(bzn_commentdraft.getUsr_user().getId())) {
					sb1.append(" and bzn.usr_user.id = '"+bzn_commentdraft.getUsr_user().getId()+"' and bzn.flag='0'  and bzn.title like '%"+bzn_commentdraft.getTitle()+"%'    order by bzn.id desc");
					sb2.append(" where bzn.usr_user.id = '"+bzn_commentdraft.getUsr_user().getId()+"' and bzn.flag='0'and bzn.title like '%"+bzn_commentdraft.getTitle()+"%' ");
				}
				
				
			}
			List<Object[]> resultlist = (List<Object[]>) getPageList(
					page, sb1.toString());
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			JSONArray newJson = new JSONArray();
			newJson.put(getNum(page, sb2.toString()));
			for (int i = 0; i < json.length(); i++) {
				newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public JsonData selectBzn_commentdraftById(Bzn_commentdraft bzn_commentdraft) {
		try{ 
			List<Object[]> resultlist = (List<Object[]>)this.selectDataByHQL( "select bzn.title , bzn.content , bzn.id , bzn.usr_user.id,bzn.usr_user.username ,bzn.bsc_category.id from Bzn_commentdraft bzn where bzn.id='"+bzn_commentdraft.getId()+"'");
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			System.out.println(json);
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(json.toString());
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
}
